var app = getApp();
Component({
    properties: {
        goods : Object,
        params : Object,
        istag : Number,
        size : String,
        ispusher : Number,
        liveid : Number,
        btntype : String,
    },
    data: {
        itemidx : 1,
    },
    options : {
        addGlobalClass : true,
    },
    methods: {
        navigateto : function( e ){
            var self = this;
            if( !self.data.params || self.data.params.indexmes == 1 || !wx.requestSubscribeMessage || !self.data.params.submess ){
                app.com.navigateto( e );
            }else{
                wx.requestSubscribeMessage({
                    tmplIds : self.data.params.submess,
                    complete : function(){
                        app.com.navigateto( e );
                    }
                })
            }
        },
        setCartnum(e){
            this.triggerEvent('setCartnum',e.detail);
        },
        dellive(e){
            var self = this;
            var i = e.currentTarget.dataset.i;
            var postdata = {
                op : 'dellivegoods',
                gid : this.data.goods[i].id,
                liveid :  this.data.liveid,
            };
            app.com.confirm('移除后将不会显示，确定要移除吗？',function(){
                app.com.http('live','POST',postdata,0,true,'','',function(res){
                    if ( !res.data.errno && res.data.errno !== undefined ) {
                        self.data.goods.splice(i,1);
                        self.setData({
                            goods: self.data.goods
                        });
                        app.com.sendGo(res.data.data.mess,res.data.data.channel);
                        app.com.toast(res.data.message);
                    }else{
                        app.com.alert(res.data.message);
                    }
                });
            });
        },
        hidelive(e){
            var self = this;
            var i = e.currentTarget.dataset.i;
            var type = e.currentTarget.dataset.type;
            var postdata = {
                op : 'hidelivegoods',
                gid : this.data.goods[i].id,
                liveid :  this.data.liveid,
                type : type,
            };
            var tip = '显示后所有用户可见此商品，';
            if(postdata.type*1 == 2){
                tip = '隐藏后用户不可看见此商品，';
            }
            app.com.confirm(tip+'确定要操作吗？',function(){
                app.com.http('live','POST',postdata,0,true,'','',function(res){
                    if ( !res.data.errno && res.data.errno !== undefined ) {

                        self.setData({
                            ['goods['+i+'].ishidelive'] : type == 1 ? false : true
                        });
                        app.com.sendGo(res.data.data.mess,res.data.data.channel);
                        app.com.toast(res.data.message);
                    }else{
                        app.com.alert(res.data.message);
                    }
                });
            });
        },
        tuijian(e){
            var self = this;
            var i = e.currentTarget.dataset.i;
            var postdata = {
                op : 'pushgoods',
                gid : this.data.goods[i].id,
                liveid :  this.data.liveid,
                type : e.currentTarget.dataset.type
            };

            var strr = '曝光后会在直播间上部显示，确定曝光吗？';
            if(postdata.type*1 == 1){
                var strr = '确定取消曝光吗？';
            }

            app.com.confirm(strr,function(){
                app.com.http('live','POST',postdata,0,true,'','',function(res){
                    if ( !res.data.errno && res.data.errno !== undefined ) {
                        self.setData({
                            goods: self.data.goods
                        });
                        for (var k = 0; k < self.data.goods.length; k++) {
                            var isact = 0;
                            if(k*1 == i*1 && postdata.type*1 == 0){
                                isact = 1;
                            }
                            self.setData({
                                ['goods['+k+'].isact'] : isact,
                            })
                        }
                        if(res.data.data.mess){
                            app.com.sendGo(res.data.data.mess,res.data.data.channel);
                        }
                        app.com.toast(res.data.message);
                    }else{
                        app.com.alert(res.data.message);
                    }
                });
            });
        }
    }
});